package com.activenetwork.util;

import java.util.List;
import java.util.Set;

import org.openqa.selenium.WebDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.collections.Lists;

public class WindowLoader {
	private Logger logger = LoggerFactory.getLogger(WindowLoader.class);
	private String defaultHandle;
	
	public WindowLoader(String handle){
		this.defaultHandle = handle;
	}
	
	public void switchWindow(WebDriver driver){
		Set<String> handles = driver.getWindowHandles();
		handles.remove(defaultHandle);
		if(handles.size() > 1){
			logger.info("Popup handles:" + handles + ". Switch to 1st handle window");
		}
		List<String> handleList = Lists.newArrayList(handles);
		switchWindow(driver, handleList.get(0));
	}
	
	public void switchWindow(WebDriver driver, String handle){
		driver.switchTo().window(handle);
	}
	
	public void switchDefault(WebDriver driver){
		driver.switchTo().window(defaultHandle);
	}

}
